package com.app.buss.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.app.buss.R;
import com.app.buss.bean.NewsBean;
import com.app.buss.ui.Forweb2Activity;
import com.app.buss.ui.ForwebActivity;
import com.bumptech.glide.Glide;

import java.util.List;

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.MViewHolder> {
    private List<NewsBean.ArticlesBean> datas;
    private Context context;

    public NewsAdapter(List<NewsBean.ArticlesBean> datas, Context context){
     this.datas = datas;
     this.context = context;
    }

    @NonNull
    @Override
    public MViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news, parent, false);
        return new MViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull MViewHolder holder, int position) {
        Glide.with(context).load(datas.get(position).getUrlToImage()).into(holder.cover);
        holder.title.setText(datas.get(position).getTitle());
        holder.tv_time.setText(datas.get(position).getPublishedAt());
        holder.des.setText(datas.get(position).getDescription());
        holder.num.setText(datas.get(position).getAuthor());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                context.startActivity(new Intent(context, Forweb2Activity.class)
                        .putExtra("title",datas.get(position).getTitle())
                        .putExtra("url",datas.get(position).getUrl()));
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    static class MViewHolder extends RecyclerView.ViewHolder{
        ImageView cover;
        TextView title;
        TextView tv_time;
        TextView des;
        TextView num;

        MViewHolder(@NonNull View itemView) {
            super(itemView);
            cover = itemView.findViewById(R.id.img_cover);
            tv_time = itemView.findViewById(R.id.tv_time);
            title = itemView.findViewById(R.id.tv_title);
            des = itemView.findViewById(R.id.tv_des);
            num = itemView.findViewById(R.id.tv_num);

        }
    }
}
